package com.tc.controller;

import com.tc.dto.Patient_service_orderDTO;
import com.tc.dto.Patient_service_orderQueryDTO;
import com.tc.service.IPatient_service_orderService;
import com.tc.utils.ResultVo;
import com.tc.vo.PageResultVO;
import com.tc.vo.Patient_service_orderVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 患者诊疗服务医嘱前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/patient_service_order")
@Api(tags = "患者诊疗服务医嘱管理")
public class Patient_service_orderController {
    
    @Autowired
    private IPatient_service_orderService serviceOrderService;

    @ApiOperation(value = "分页查询患者诊疗服务医嘱")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Patient_service_orderVO>> pageQuery(@RequestBody Patient_service_orderQueryDTO queryDTO) {
        PageResultVO<Patient_service_orderVO> pageResultVO = serviceOrderService.pageQuery(queryDTO);
        return ResultVo.ok(pageResultVO);
    }
    
    @ApiOperation(value = "根据患者ID查询诊疗服务医嘱列表")
    @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/patient/{patientId}")
    public ResultVo<List<Patient_service_orderVO>> getByPatientId(@PathVariable Long patientId) {
        List<Patient_service_orderVO> list = serviceOrderService.getByPatientId(patientId);
        return ResultVo.ok(list);
    }

    @ApiOperation(value = "根据ID查询患者诊疗服务医嘱")
    @ApiImplicitParam(name = "id", value = "医嘱ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public ResultVo<Patient_service_orderVO> getById(@PathVariable Long id) {
        Patient_service_orderVO vo = serviceOrderService.getServiceOrderById(id);
        if (vo != null) {
            return ResultVo.ok(vo);
        } else {
            return ResultVo.fail("诊疗服务医嘱不存在");
        }
    }

    @ApiOperation(value = "新增患者诊疗服务医嘱")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody @Valid Patient_service_orderDTO orderDTO) {
        boolean success = serviceOrderService.addServiceOrder(orderDTO);
        if (success) {
            return ResultVo.ok(true, "新增成功");
        } else {
            return ResultVo.fail("新增失败");
        }
    }

    @ApiOperation(value = "修改患者诊疗服务医嘱")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody @Valid Patient_service_orderDTO orderDTO) {
        if (orderDTO.getOrderId() == null) {
            return ResultVo.fail("医嘱ID不能为空");
        }
        
        boolean success = serviceOrderService.updateServiceOrder(orderDTO);
        if (success) {
            return ResultVo.ok(true, "修改成功");
        } else {
            return ResultVo.fail("修改失败");
        }
    }

    @ApiOperation(value = "删除患者诊疗服务医嘱")
    @ApiImplicitParam(name = "id", value = "医嘱ID", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultVo<Boolean> delete(@PathVariable Long id) {
        boolean success = serviceOrderService.deleteServiceOrder(id);
        if (success) {
            return ResultVo.ok(true, "删除成功");
        } else {
            return ResultVo.fail("删除失败");
        }
    }
    
    @ApiOperation(value = "批量删除患者诊疗服务医嘱")
    @DeleteMapping("/batch")
    public ResultVo<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = serviceOrderService.batchDeleteServiceOrder(ids);
        if (success) {
            return ResultVo.ok(true, "批量删除成功");
        } else {
            return ResultVo.fail("批量删除失败");
        }
    }
    

}

